package com.tju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.eduservice.client.VodClient;
import com.tju.eduservice.entity.EduVideo;
import com.tju.eduservice.mapper.EduVideoMapper;
import com.tju.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;


    @Override
    public void removeVideoByCourseId(String id) {
        //根据课程id查询视频id
        QueryWrapper<EduVideo> eduVideoWrapper = new QueryWrapper<>();
        eduVideoWrapper.eq("course_id",id);
        eduVideoWrapper.select("video_source_id");//只查询一个字段
        List<EduVideo> eduVideoList = baseMapper.selectList(eduVideoWrapper);
        ArrayList<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : eduVideoList) {
            if(eduVideo != null && !StringUtils.isEmpty(eduVideo.getId())){
                videoIds.add(eduVideo.getId());
            }
        }

        vodClient.deleteBatch(videoIds);


        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
